package org.jeecg.modules.demo.jxcx.controller;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.demo.jxcx.Utils.R;
import org.jeecg.modules.demo.jxcx.po.JxcXxfb;
import org.jeecg.modules.demo.jxcx.service.JxcXxfbService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping({"jxcXxfb"})
public class JxcXxfbController {
    private static Logger logger = LoggerFactory.getLogger(JxcXxfbController.class);
    @Autowired
    private JxcXxfbService jxcXxfbService;
    @PostMapping({"/queryall"})
    @ResponseBody
    public R queryall(@RequestBody JSONObject params) {
        return new R().ok(jxcXxfbService.queryall());
    }

    @PostMapping({"add"})
    public R insert(@RequestBody JxcXxfb jxcXxfb) {
        return jxcXxfbService.add(jxcXxfb);
    }
    @PutMapping("update")
    public R update(@RequestBody JxcXxfb jxcXxfb) {
        String id = jxcXxfb.getId();
        JxcXxfb byId = jxcXxfbService.getById(id);
        String llnums = Long.valueOf(byId.getLlnums()) + 1 + "";
        String dianzan1 = Long.valueOf(byId.getDianzan()) + 1 + "";
        String dianzan2 = Long.valueOf(byId.getDianzan()) - 1 + "";
        if (StringUtils.isNotBlank(jxcXxfb.getDianzan())){
            if (jxcXxfb.getDianzan().equals("点赞")){
                jxcXxfb.setDianzan(dianzan1);
                jxcXxfb.setAnother("true");
            }else if (jxcXxfb.getDianzan().equals("取消点赞")){
                jxcXxfb.setDianzan(dianzan2);
                jxcXxfb.setAnother("false");
            }
        }
        if (StringUtils.isNotBlank(jxcXxfb.getLlnums())){
            if (jxcXxfb.getLlnums().equals("浏览量")){
                jxcXxfb.setLlnums(llnums);
            }
        }

        return new R().ok(jxcXxfbService.updateById(jxcXxfb));
    }
}
